package edu.gdkm.controller;

import com.github.pagehelper.PageHelper;
import edu.gdkm.entity.PageResult;
import edu.gdkm.entity.Result;
import edu.gdkm.pojo.Book;
import edu.gdkm.pojo.User;
import edu.gdkm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public String login(User user, HttpServletRequest request){
        try {
            User u = userService.login(user);
            if (u != null) {
                request.getSession().setAttribute("USER_SESSION", u);
                return "redirect:/admin/main.jsp";
            }
            request.setAttribute("msg", "用户名或者密码错误");
            return "forward:/admin/login.jsp";
        }catch (Exception e){
            request.setAttribute("msg","系统错误");
            return "forward:/admin/login.jsp";
        }
    }
    @RequestMapping("/toMainPage")
    public String toMainPage(){
        return "main";
    }
    /*
   注销登录
    */
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request) {
        try {
            HttpSession session = request.getSession();
            //销毁Session
            session.invalidate();
            return "forward:/admin/login.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "系统错误");
            return "forward:/admin/login.jsp";
        }
    }
    /**
     * 新增用户
     * @param user 新增的用户信息
     */
    @ResponseBody
    @RequestMapping("/addUser")
    public Result addUser(User user) {
        try {
            Integer count=userService.addUser(user);
            if(count!=1){
                return new Result(false, "新增用户失败!");
            }
            return new Result(true, "新增用户成功!");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, "新增用户失败!");
        }
    }

    /**
     * 办理用户离职
     * @param id 离职的用户id
     */
    @ResponseBody
    @RequestMapping("/delUser")
    public Result delUser(Integer id) {
        try {
            Integer count=userService.delUser(id);
            if(count!=1){
                return new Result(false, "删除用户失败!");
            }
            return new Result(true, "删除用户成功!");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, "删除用户失败!");
        }
    }

    /**
     * 修改用户信息
     * @param user 修改的用户信息
     */
    @ResponseBody
    @RequestMapping("/editUser")
    public Result editBook(User user) {
        try {
            Integer count= userService.editUser(user);
            if(count!=1){
                return new Result(false, "编辑失败!");
            }
            return new Result(true, "编辑成功!");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, "编辑失败!");
        }
    }

    /**
     * 查询用户
     * @param user 查询的条件
     * @param pageNum  数据列表的当前页码
     * @param pageSize 数据列表1页展示多少条数据
     */
    @RequestMapping("/search")
    public ModelAndView search(User user, Integer pageNum, Integer pageSize,HttpServletRequest request) {
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 6;
        }
        //查询到的图书信息
        PageHelper.startPage(pageNum, pageSize);
        PageResult pageResult = userService.search(user, pageNum, pageSize);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("user");
        //将查询到的数据存放在 ModelAndView的对象中
        modelAndView.addObject("pageResult", pageResult);
        //将查询的参数返回到页面，用于回显到查询的输入框中
        modelAndView.addObject("search", user);
        //将当前页码返回到页面，用于分页插件的分页显示
        modelAndView.addObject("pageSize", pageSize);
        modelAndView.addObject("pageNum", pageNum);
        //将当前查询的控制器路径返回到页面，页码变化时继续向该路径发送请求
        modelAndView.addObject("gourl", request.getRequestURI());
        return modelAndView;
    }

    /**
     * 根据id查询用户
     *
     * @param id 用户id，用作查询条件
     */
    @ResponseBody
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            User user=userService.findById(id);
            if(user==null){
                return new Result(false,"查询用户失败！");
            }
            return new Result(true,"查询用户成功",user);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询用户失败！");
        }
    }

    /**
     * 检查用户名称是否已经存在
     *
     * @param name 用户名称
     */
    @ResponseBody
    @RequestMapping("/checkName")
    public Result checkName(String name) {
        try {
            Integer user=userService.checkName(name);
            if(user==null){
                return new Result(false,"没有相同用户！");
            }
            return new Result(true,"已存在同名用户",user);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询失败！");
        }
    }

    /**
     * 校验用户的邮箱是否已经存在
     * @param email 被校验的用户邮箱
     */
    @ResponseBody
    @RequestMapping("/checkEmail")
    public Result checkEmail(String email) {
        try {
            Integer user=userService.checkEmail(email);
            if(user==null){
                return new Result(false,"邮箱未重复");
            }
            return new Result(true,"邮箱已存在重复",user);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询失败！");
        }
    }

}
